package elsa.controller;

import com.github.pagehelper.PageHelper;
import elsa.bean.ScsLog;
import elsa.bootmapper.ConstMapper;
import elsa.bootmapper.LogMapper;
import elsa.bootmapper.OpsMapper;
import elsa.tools.Message;
import elsa.tools.Method;
import elsa.tools.MyResp;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class LogController {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private ConstMapper constMapper;

    @Autowired
    private OpsMapper opsMapper;

    //查询日志列表 兼 分页
    @CrossOrigin
    @RequestMapping(value = {"/project/const/log/{lognum}", "/project/ops/log/{lognum}"}, method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("surveyor")
    public MyResp logAllList(@PathVariable("lognum") String lognum,
                             @RequestParam("pageIndex") Integer pageIndex,
                             @RequestParam("pageSize") Integer pageSize) {
        if(lognum==null){
            lognum="";
        }
        if(pageIndex==null){
            pageIndex=1;
        }
        if(pageSize==null){
            pageSize=10;
        }
        PageHelper.startPage(pageIndex, pageSize);
        return MyResp.render(0, "获取成功", logMapper.findAllLog(lognum), logMapper.findLogCount(lognum));
    }

    //查询日志信息
    @CrossOrigin
    @RequestMapping(value = {"/project/const/log/info", "/project/ops/log/info"}, method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("surveyor")
    public ScsLog logInfo(@RequestParam("lognum") String lognum, @RequestParam("logtime") String logtime) {
        if(lognum==null){
            lognum="";
        }
        if(logtime==null){
            logtime="";
        }
        return logMapper.findLogInfo(lognum, logtime);
    }

    //打印日志信息
    @CrossOrigin
    @RequestMapping(value = {"/project/const/log/print", "/project/ops/log/print"}, method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("surveyor")
    public ScsLog printLog(@RequestParam("lognum") String lognum, @RequestParam("logtime") String logtime) {
        if(lognum==null){
            lognum="";
        }
        if(logtime==null){
            logtime="";
        }
        return logMapper.printLogInfo(lognum, logtime);
    }

    //新建日志
    @CrossOrigin
    @RequestMapping(value = {"/project/const/log/new", "/project/ops/log/new"}, method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("surveyor")
    public Message addLog(@RequestBody ScsLog scsLog) {
        return new Message(setAddLog(scsLog));
    }

    //更新日志
    @CrossOrigin
    @RequestMapping(value = {"/project/const/log/update", "/project/ops/log/update"}, method = RequestMethod.PUT)
    @ResponseBody
    @RequiresPermissions("surveyor")
    public Message updateLog(@RequestBody ScsLog scsLog){
        return new Message(setUpdateLog(scsLog));
    }

    //删除日志
    @CrossOrigin
    @RequestMapping(value = {"/project/const/log/delete", "/project/ops/log/delete"}, method = RequestMethod.DELETE)
    @ResponseBody
    @RequiresPermissions("surveyor")
    public Message deleteLog(@RequestParam("lognum") String lognum, @RequestParam("logtime") String logtime){
        return new Message(setDeleteLog(lognum, logtime));
    }


    /**
     * 日志操作
     */
    //新建日志
    private String setAddLog(ScsLog scsLog) {
        String b;
        if (scsLog.getLognum()==null||(constMapper.findConstByInspnum(scsLog.getLognum()) == null && opsMapper.findOpsByRgstnum(scsLog.getLognum()) == null)) {
            b = "此项目不存在!";
        }else if(scsLog.getLogname()==null||!chickName(scsLog.getLogname())){
            b = "日志名称格式填写错误!";
        }else if(scsLog.getInsptype()==null||!chickInsptype(scsLog.getInsptype())){
            b = "检验类型格式填写错误!";
        }else if(scsLog.getAddress()==null||!chickName(scsLog.getAddress())){
            b ="检验地点格式填写错误!";
        }else{
            Date date = new Date();
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = format.format(date);
            scsLog.setLogtime(str);
            scsLog.setLatesttime(getNtime());
            Method method = new Method();
            DateFormat dformat = new SimpleDateFormat("yyyyMMddHHmmss");
            String t = dformat.format(date);
            scsLog.setTtime(t);
            String path;
            if(constMapper.findConstByInspnum(scsLog.getLognum())!=null){
                path = "const/"+scsLog.getLognum()+"/constLog"+t;
                scsLog.setInsptype("建造检验");
            }else{
                path = "ops/"+scsLog.getLognum()+"/opsLog"+t;
                scsLog.setInsptype("营运检验");
            }
            if(!scsLog.getLogpicture1().equals("")){
                method.copyFile(scsLog.getLogpicture1(),path+scsLog.getLogpicture1().substring(scsLog.getLogpicture1().lastIndexOf("/")));
                scsLog.setLogpicture1(path+scsLog.getLogpicture1().substring(scsLog.getLogpicture1().lastIndexOf("/")));
            }
            if(!scsLog.getLogpicture2().equals("")){
                method.copyFile(scsLog.getLogpicture2(),path+scsLog.getLogpicture2().substring(scsLog.getLogpicture2().lastIndexOf("/")));
                scsLog.setLogpicture2(path+scsLog.getLogpicture2().substring(scsLog.getLogpicture2().lastIndexOf("/")));
            }
            if(!scsLog.getLogpicture3().equals("")){
                method.copyFile(scsLog.getLogpicture3(),path+scsLog.getLogpicture3().substring(scsLog.getLogpicture3().lastIndexOf("/")));
                scsLog.setLogpicture3(path+scsLog.getLogpicture3().substring(scsLog.getLogpicture3().lastIndexOf("/")));
            }
            if(!scsLog.getLogpicture4().equals("")){
                method.copyFile(scsLog.getLogpicture4(),path+scsLog.getLogpicture4().substring(scsLog.getLogpicture4().lastIndexOf("/")));
                scsLog.setLogpicture4(path+scsLog.getLogpicture4().substring(scsLog.getLogpicture4().lastIndexOf("/")));
            }
            if(!scsLog.getLogpicture5().equals("")){
                method.copyFile(scsLog.getLogpicture5(),path+scsLog.getLogpicture5().substring(scsLog.getLogpicture5().lastIndexOf("/")));
                scsLog.setLogpicture5(path+scsLog.getLogpicture5().substring(scsLog.getLogpicture5().lastIndexOf("/")));
            }
            if(!scsLog.getLogpicture6().equals("")){
                method.copyFile(scsLog.getLogpicture6(),path+scsLog.getLogpicture6().substring(scsLog.getLogpicture6().lastIndexOf("/")));
                scsLog.setLogpicture6(path+scsLog.getLogpicture6().substring(scsLog.getLogpicture6().lastIndexOf("/")));
            }
            if(!scsLog.getLogpicture7().equals("")){
                method.copyFile(scsLog.getLogpicture7(),path+scsLog.getLogpicture7().substring(scsLog.getLogpicture7().lastIndexOf("/")));
                scsLog.setLogpicture7(path+scsLog.getLogpicture7().substring(scsLog.getLogpicture7().lastIndexOf("/")));
            }
            if(!scsLog.getLogpicture8().equals("")){
                method.copyFile(scsLog.getLogpicture8(),path+scsLog.getLogpicture8().substring(21));
                scsLog.setLogpicture8(path+scsLog.getLogpicture8().substring(21));
            }
            if(!scsLog.getLogvideo1().equals("")){
                method.copyFile(scsLog.getLogvideo1(),path+scsLog.getLogvideo1().substring(scsLog.getLogvideo1().lastIndexOf("/")));
                scsLog.setLogvideo1(path+scsLog.getLogvideo1().substring(scsLog.getLogvideo1().lastIndexOf("/")));
            }
            if(!scsLog.getLogvideo2().equals("")){
                method.copyFile(scsLog.getLogvideo2(),path+scsLog.getLogvideo2().substring(scsLog.getLogvideo2().lastIndexOf("/")));
                scsLog.setLogvideo2(path+scsLog.getLogvideo2().substring(scsLog.getLogvideo2().lastIndexOf("/")));
            }
            logMapper.saveLog(scsLog);
            b = "日志新建成功!";
        }
        return b;
    }

    //更新日志
    private String setUpdateLog(ScsLog scsLog) {
        String b;
        if(scsLog.getLognum()==null||scsLog.getLogtime()==null||logMapper.findLogInfo(scsLog.getLognum(), scsLog.getLogtime())==null){
            b = "日志不存在!";
        }else if(scsLog.getLogname()==null||!chickName(scsLog.getLogname())){
            b = "日志名称格式填写错误!";
        }else if(scsLog.getLogname()==null||!chickName(scsLog.getLogname())){
            b = "检验地点格式填写错误!";
        }else{
            Date date = new Date();
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = format.format(date);
            scsLog.setAtime(str);
            scsLog.setLatesttime(getNtime());
            logMapper.updateLog(scsLog);
            b = "日志更新成功!";
        }
        return b;
    }

    //删除日志
    private String setDeleteLog(String lognum, String logtime){
        String b;
        if(lognum==null||logtime==null||logMapper.findLogInfo(lognum, logtime)==null){
            b = "该日志不存在!";
        }else {
            String t = logMapper.findLogInfo(lognum, logtime).getTtime();
            String path;
            if(constMapper.findConstByInspnum(lognum)!=null){
                path = "const/"+lognum+"/constLog"+t;
            }else{
                path = "ops/"+lognum+"/opsLog"+t;
            }
            String classPath = this.getClass().getClassLoader().getResource("").getPath();
            String thepath = classPath.substring(0,classPath.indexOf("WEB-INF"))+"file/";
            path = thepath+path;
            File dir = new File(path);
            System.out.println(path);
            Method method = new Method();
            System.out.println(method.deleteDir(dir));//删除所有文件
            logMapper.deleteLog(lognum, logtime);
            b = "删除日志成功!";
        }
        return b;
    }

    /**
     * 其他操作
     */
    private boolean chickName(String name) {
        Pattern pattern = Pattern.compile("^[\u4E00-\u9FA5A-Za-z0-9]{1,20}$");
        Matcher match = pattern.matcher(name);
        return match.matches();
    }

    //检验检验类型
    private boolean chickInsptype(String insptype) {
        Boolean b = false;
        if(insptype.equals("建造检验")||insptype.equals("营运检验")){
            b = true;
        }
        return b;
    }

    //获取当前时间
    private Date getNtime(){
        Date date = new Date();
        return new Timestamp(date.getTime());
    }

}